#include "client.h"
#include <my_header.h>
int main(int argc, char *argv[]) {
    // 读取配置文件
    char ip[20] = {0};
    char port[8] = {0};
    configureInit(ip, port);

    // 建立间接
    /* ARGS_CHECK(argc, 3); */
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    ERROR_CHECK(sockfd, -1, "socket");
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(port));
    addr.sin_addr.s_addr = inet_addr(ip);
    int ret = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
    printf("%d:connct成功返回0;其他为connect失败\n", ret);
    // 工作逻辑

    userInfo_t user;
    ret = logIn(&sockfd, &user);
    printf("%d\n", ret);
    while (getchar() != '\n')
        ;

    // 任务2 命令解析，并按照cmd内容进入对应接口
    cmdInfo_t cmdinfo;
    memset(&cmdinfo, 0, sizeof(cmdInfo_t));
    strcpy(cmdinfo.cmdPath, "/");
    while (1) {
        char command[4096] = {0}; // 即 cd xxxxxx
        printf("%s@:%s$", user.userName, cmdinfo.cmdPath);
        readCommand(command, sizeof(command)); // 从stdin读取指令，并取出换行符
        // 此时，command内已经存好了从stdin输入的命令,且不包含换行符
        char arg[4096] = {0};     // 即 cd xxxxxx中的“xxxxx”部分
        char cmd_str[1024] = {0}; // 即cd
        analyzeCommand(
            command, sizeof(command), cmd_str, sizeof(cmd_str), arg,
            sizeof(
                arg)); // 解析指令，去除无用的空格和换行符，把cmd、arg分离出来，
        /* printf("cmd = %s\n",cmd);
        printf("arg = %s\n",arg); */
        cmd_t cmd = matchCmd(cmd_str);

        // 创建命令结构体并初始化

        strcpy(cmdinfo.arg, arg);
        strcpy(cmdinfo.arg, arg);
        cmdinfo.cmd = cmd;
        cmdinfo.argLength = strlen(cmdinfo.arg);
        strcpy(
            cmdinfo.userName,
            user.userName); // 暂时设置为zs，后面要改！--------------------------------------------------------------------------
        strcpy(cmdinfo.fileName, "xiaoyu");
        /*  sprintf(cmdinfo.cmdPath,"/home/%s",cmdinfo.userName); */
        // 结构体还没初始化完--------------------------------------------------------------------------------------------------------------
        printf("----------------------from :client.c----------------------------\ncmdinfo.cmd = %d\ncmdinfo.userName = %s\ncmdinfo.arg = "
               "%s\ncmdinfo.argLength = %d\n",
               cmdinfo.cmd, cmdinfo.userName, cmdinfo.arg, cmdinfo.argLength);
        findCmdInterface(sockfd, cmd,
                         &cmdinfo); // 根据cmd，将程序转向对应的接口
    }
    close(sockfd);
    return 0;
}
int configureInit(char *ip, char *port) {

    char str[256];
    FILE *configFile = fopen("/configure/netdisk.ini", "r");
    if (configFile == NULL) {
        printf("Failed to open the configuration file!\n");
        exit(-1);
    }
    memset(str, 0, sizeof(str));
    fgets(str, 256, configFile);
    str[strcspn(str, "\n")] = '\0';
    char *p = strstr(str, "=");
    if (p != NULL) {
        p++;
        sprintf(ip, "%s", p);
        printf("ip = %s", ip);
    } else {
        printf("not found =\n");
    }
    memset(str, 0, sizeof(str));
    fgets(str, 256, configFile);
    str[strcspn(str, "\n")] = '\0';
    p = strstr(str, "=");
    if (p != NULL) {
        p++;
        sprintf(port, "%s", p);
        printf("port = %s", port);
    } else {
        printf("not found =\n");
    }
}
